var statusaction = {
	init : function(){
		$('table.tasknotification').children('tbody').children('tr').each(function(index, elmt){
			var btn = $(this).children('td.action').children('button');
			
			btn.on('click', function(event){
				statusaction.dismiss_notification(btn.attr('data-id'));
			});
		});
	},
	
	dismiss_notification : function(notification_id){
		var elmt, postdata;
		
		postdata = 'notification_id=' + encodeURIComponent(notification_id);
		
		if(confirm('Confirm this task has been completed?')){
			var callback = function(json){
	            if(json.status == 'OK'){
	            	mapUI.jq_message("Task item updated successfully.", 10);
	            	window.location.reload();
					
	            } else {
	            	mapUI.jq_message(json.message, 10);
	            }
	        };
			
	        var jq = {
	            'method' : 'POST',
	            'url' : jq_main_url + '/SubmitApp.do?dispatch=notification_action',
	            'postdata' : postdata
	        };
			
	        mapUI.jq_call(jq, callback);
		}
	},
	
	submit : function(case_id){
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.caseaction.status_note;
		postdata += '&status_note=' + encodeURIComponent(elmt.value);
		
		elmt = document.caseaction.status_action;
		postdata += '&status_action=' + encodeURIComponent(elmt.value);
		
		var status_group = $('#status_group').val();
		
		if(status_group != null){
			postdata += '&status_group=' + encodeURIComponent(JSON.stringify(status_group));
		}
		else {
			postdata += '&status_group=';
		}
		
		elmt = document.caseaction.status_email;
		if($(elmt).is(':checked')){
			postdata += '&status_email=' + encodeURIComponent(elmt.value);
		} else {
			postdata += '&status_email=';
		}
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Status action updated successfully.", 10);
            	window.location.reload();
				
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=status_action',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	},
	
	submit2 : function(case_id){
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.extremark.status_action2;
		postdata += '&status_action2=' + encodeURIComponent(elmt.value);
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Terminal Installation Status action updated successfully.", 10);
            	window.location.reload();
				
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=status_action2',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	},
	
	extremark_submit : function(case_id){
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.extremark.external_remark;
		postdata += '&external_remark=' + encodeURIComponent(elmt.value);
		
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("External remark updated successfully.", 10);
            	window.location.reload();
				
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=extremark_action',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	},
	
	update_cardlink : function(case_id){
		var elmt, postdata;
		
		
		var regexp = /^\s*-?[0-9]\d*(\.\d{1,2})?\s*$/
			
		var gst_rate_elmts = document.getElementsByClassName("gst_rate");
		var i;
		for (i = 0; i < gst_rate_elmts.length; i++) {
			
			if (gst_rate_elmts[i].value == '') {
				mapUI.jq_message('GST Rate must not be blank.', 10);
				return;
			}
			if(regexp.test(gst_rate_elmts[i].value) == false){
				mapUI.jq_message('GST Rate must be a numeric value.', 10);
				return;
			}
		}
		
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.getElementById('case_outlet_opt');
		postdata += '&outlet_id=' + encodeURIComponent(elmt.value);
		
		var start = function(){
			mapUI.jq_message("Updating Card Link ...");
		};
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Update Card Link completed successfully.", 10);
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=update_cardlink',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback, start);
	},
	
	clear_cardlink : function(case_id){
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.getElementById('case_outlet_opt');
		postdata += '&outlet_id=' + encodeURIComponent(elmt.value);
		
		var start = function(){
			mapUI.jq_message("Updating Card Link ...");
		};
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Clear Card Link completed successfully.", 10);
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=clear_cardlink',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback, start);
	}
}